<template>
  <el-dialog
    :title="showTitle"
    :visible.sync="showdialogTags"
    width="30%"
    @close="btnCancel"
  >
    <el-form
      :model="ruleForm"
      ref="ruleForm"
      label-width="100px"
      :rules="rules"
    >
      <el-form-item label="所属学科">
        <el-select
          v-model="ruleForm.subjectID"
          placeholder="请选择"
          @focus="subjectListSimple"
        >
          <el-option
            v-for="item in options"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="标签名称" prop="tagName">
        <el-input
          v-model="ruleForm.tagName"
          placeholder="请输入标签名称"
        ></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="btnCancel">取 消</el-button>
      <el-button type="primary" @click="btnOk">确 定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { subjectListSimple } from "@/api/subjects/subjects";
import {addTags,editTags,tagsDetail} from '@/api/subjects/tags'
export default {
  name: "dialogTags",
  data() {
    return {
      showdialogTags: false,
      ruleForm: {
        id: "",
        subjectID: "",
        tagName: "",
      },

      options: [],
      rules: {
        tagName: [
          { required: true, message: "标签名称不能为空", trigger: "blur" },
          {
            min: 1,
            max: 10,
            message: "标签名称要求1-10个字符",
            trigger: "blur",
          },
        ],
      },
    };
  },

  computed: {
    showTitle() {
      // id默认空串还是id.length
      return this.ruleForm.id ? "编辑标签" : "新增标签";
    },
  },
  methods: {
    // 取消按钮
    btnCancel() {
      this.$refs.ruleForm.resetFields();
      this.ruleForm = {
        id: "",
        subjectID: "",
        tagName: "",
      };
      this.showdialogTags = false;
    },
    //确定按钮
    async btnOk() {
      // 主动校验
      await this.$refs.ruleForm.validate()
      if (this.ruleForm.id) {
        //  编辑
        console.log(this.ruleForm);
        await editTags(this.ruleForm);
      } else {
        //  新增
        await addTags(this.ruleForm);

      }
      // 成功提示
      this.$message.success("操作成功");
      // 更新列表
      this.$emit("updateTags");
      // 关闭弹层
      this.showdialogTags = false;
    },
    // 数据回显，下拉选择框
    async subjectListSimple() {
      const { data } = await subjectListSimple();
      this.options = data;
    },

    // 修改的数据回显
    async getTagsDetail(id) {
      //  获取简单列表,回显的id，名称问题
      await this.subjectListSimple();
      const { data } = await tagsDetail(id);
      console.log(data);
      this.ruleForm.id = data.id;
      this.ruleForm.subjectID = data.subjectID;
      this.ruleForm.tagName = data.tagName;
    },
  },
};
</script>

<style scoped>
::v-deep .el-dialog__header {
  background-color: #43a4ff;
}
::v-deep .el-dialog__title {
  color: #fff;
}
 ::v-deep .el-icon-close:before{
  color: #fff;

}
</style>
